package org.jeecg.modules.tplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.goods.goods.entity.Goods;
import org.jeecg.modules.goods.goods.service.IGoodsService;
import org.jeecg.modules.tplus.dto.*;
import org.jeecg.modules.tplus.dto.query.OneClickQuery;
import org.jeecg.modules.tplus.service.DatabaseService;
import org.jeecg.modules.tplus.service.ITplusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: t+接口管理
 * @Author: jeecg-boot
 * @Date: 2025-06-07
 * @Version: V1.0
 */
@Slf4j
@Tag(name = "t+接口管理")
@RestController
@RequestMapping("/tplus/api")
public class TplusController {
    @Autowired
    private ITplusService iTplusService;

    @Autowired
    private DatabaseService databaseService;

    /**
     * 导出存货数据
     *
     * @param param
     * @return
     */
    @AutoLog(value = "导出存货数据")
    @Operation(summary = "导出tPlus的存货数据至本地数据库")
    @PostMapping(value = "/createToDayInventoryByTPlus")
    public Result<?> createToDayInventoryByTPlus(@RequestBody OneClickQuery param) {
        iTplusService.getInventoryByTPlus(param.getStartDate(), param.getEndDate(), param.getCode(),
                param.getClassCode());
        return Result.OK("操作成功！");
    }

    @Operation(summary = "查询存货调价单", description = "根据调价单处理状态查询存货调价单")
    @PostMapping(value = "/inventoryAdjustPriceQueryByHandle")
    public Result<?> inventoryAdjustPriceQueryByHandle(@RequestBody InventoryAdjustPriceQueryReqDto inventoryAdjustPriceQueryReqDto) {
        List<InventoryAdjustPriceRepDto> inventoryAdjustPriceRepDtos = iTplusService.inventoryAdjustPriceQueryByHandle(inventoryAdjustPriceQueryReqDto);
        return Result.OK(inventoryAdjustPriceRepDtos);
    }


    @Operation(summary = "导出存货调价单", description = "导出存货调价单至本地数据库")
    @PostMapping(value = "/createInventoryAdjustPriceByTPlus")
    public Result<?> createInventoryAdjustPriceByTPlus(@RequestBody InventoryAdjustPriceQueryReqDto inventoryAdjustPriceQueryReqDto) {
        iTplusService.createInventoryAdjustPriceByTPlus(inventoryAdjustPriceQueryReqDto);
        return Result.OK("操作成功！");
    }

    /**
     * 查询存货数据
     *
     * @param inventoryQuery
     * @return
     */
    @AutoLog(value = "查询存货数据")
    @Operation(summary = "查询tPlus的存货数据")
    @PostMapping(value = "/queryInventoryByTPlus")
    public Result<?> queryInventoryByTPlus(@RequestBody InventoryQueryReqDto inventoryQuery) {
        InventoryListQueryRepDto inventoryListQueryRepDto = iTplusService.doInventoryQuery(inventoryQuery);
        return Result.OK(inventoryListQueryRepDto);
    }

    /**
     * 存货数据新增
     *
     * @param inventoryInfoReqDto
     * @return
     */
    @AutoLog(value = "存货数据新增")
    @Operation(summary = "存货数据新增")
    @PostMapping(value = "/inventoryCreate")
    public Result<?> inventoryCreate(@RequestBody InventoryInfoReqDto inventoryInfoReqDto) {
        iTplusService.inventoryCreate(inventoryInfoReqDto);
        return Result.OK("操作成功！");
    }

    /**
     * 存货数据编辑
     *
     * @param inventoryInfoReqDto
     * @return
     */
    @AutoLog(value = "存货数据编辑")
    @Operation(summary = "存货数据编辑")
    @PostMapping(value = "/inventoryUpdate")
    public Result<?> inventoryUpdate(@RequestBody InventoryInfoReqDto inventoryInfoReqDto) {
        iTplusService.inventoryUpdate(inventoryInfoReqDto);
        return Result.OK("操作成功！");
    }

    /**
     * 存货数据图片关联
     *
     * @param inventoryImageEntityReqDto
     * @return
     */
    @AutoLog(value = "存货数据图片关联")
    @Operation(summary = "存货数据图片关联")
    @PostMapping(value = "/updateImage")
    public Result<?> updateImage(@RequestBody InventoryImageEntityReqDto inventoryImageEntityReqDto) {
        iTplusService.updateImage(inventoryImageEntityReqDto);
        return Result.OK("操作成功！");
    }

    /**
     * 查询仓库
     *
     * @param warehouseQueryReqDto
     * @return
     */
    @AutoLog(value = "查询仓库")
    @Operation(summary = "查询仓库")
    @PostMapping(value = "/warehouseQuery")
    public Result<?> warehouseQuery(@RequestBody WarehouseQueryReqDto warehouseQueryReqDto) {
        List<WarehouseQueryRepDto> warehouseListQueryRepDto = iTplusService.warehouseQuery(warehouseQueryReqDto);
        return Result.OK(warehouseListQueryRepDto);
    }

    /**
     * 查询货位
     *
     * @param invLocationQueryReqDto
     * @return
     */
    @AutoLog(value = "查询货位")
    @Operation(summary = "查询货位")
    @PostMapping(value = "/invLocationQuery")
    public Result<?> invLocationQuery(@RequestBody InvLocationQueryReqDto invLocationQueryReqDto) {
        List<InvLocationQueryRepDto> invLocationQueryRepDtos = iTplusService.invLocationQuery(invLocationQueryReqDto);
        return Result.OK(invLocationQueryRepDtos);
    }

    /**
     * 查询分类
     *
     * @param inventoryClassQueryReqDto
     * @return
     */
    @AutoLog(value = "查询分类")
    @Operation(summary = "查询分类")
    @PostMapping(value = "/inventoryClassQuery")
    public Result<?> inventoryClassQuery(@RequestBody InventoryClassQueryReqDto inventoryClassQueryReqDto) {
        List<InventoryClassQueryRepDto> inventoryClassQueryRepDtos = iTplusService
                .inventoryClassQuery(inventoryClassQueryReqDto);
        return Result.OK(inventoryClassQueryRepDtos);
    }

    /**
     * 查询计量单位
     *
     * @param unitQueryReqDto
     * @return
     */
    @AutoLog(value = "查询计量单位")
    @Operation(summary = "查询计量单位")
    @PostMapping(value = "/unitQuery")
    public Result<?> unitQuery(@RequestBody UnitQueryReqDto unitQueryReqDto) {
        UnitQueryRepDto unitQueryRepDto = iTplusService.unitQuery(unitQueryReqDto);
        return Result.OK(unitQueryRepDto);
    }

    /**
     * 查询供应商
     *
     * @param partnerQueryReqDto
     * @return
     */
    @AutoLog(value = "查询供应商")
    @Operation(summary = "查询供应商")
    @PostMapping(value = "/doPartnerQuery")
    public Result<?> doPartnerQuery(@RequestBody PartnerQueryReqDto partnerQueryReqDto) {
        List<PartnerQueryRepDto> partnerQueryRepDtos = iTplusService.doPartnerQuery(partnerQueryReqDto);
        return Result.OK(partnerQueryRepDtos);
    }

    /**
     * 查询枚举
     *
     * @param enumQueryReqDto
     * @return
     */
    @AutoLog(value = "查询枚举")
    @Operation(summary = "查询枚举")
    @PostMapping(value = "/getEnumByName")
    public Result<?> getEnumByName(@RequestBody EnumQueryReqDto enumQueryReqDto) {
        EnumQueryRepDto enumQueryRepDto = iTplusService.getEnumByName(enumQueryReqDto);
        return Result.OK(enumQueryRepDto);
    }

    /**
     * 同步处理枚举
     *
     * @return
     */
    @AutoLog(value = "同步处理枚举")
    @Operation(summary = "同步处理枚举")
    @PostMapping(value = "/handleEnumByName")
    public Result<?> handleEnumByName() {
        iTplusService.handleEnum();
        return Result.OK("操作成功");
    }

    /**
     * 根据tPlusEnumName新增数据字典
     *
     * @param tPlusEnumName T+枚举名称
     * @return
     */
    @AutoLog(value = "根据tPlusEnumName新增数据字典")
    @Operation(summary = "根据tPlusEnumName新增数据字典")
    @PostMapping(value = "/createDictByEnumName")
    public Result<?> createDictByEnumName(@RequestParam String tPlusEnumName) {
        try {
            iTplusService.createDictByEnumName(tPlusEnumName);
            return Result.OK("字典创建成功");
        } catch (Exception e) {
            return Result.error("字典创建失败: " + e.getMessage());
        }
    }

    /**
     * 获取token
     *
     * @param refreshTokenReqDto
     * @return
     */
    @AutoLog(value = "获取token")
    @Operation(summary = "获取token")
    @PostMapping(value = "/getToken")
    public Result<?> getToken(@RequestBody RefreshTokenReqDto refreshTokenReqDto) {
        iTplusService.getToken(refreshTokenReqDto);
        return Result.OK("操作成功！");
    }

    /**
     * 同步存货分类到系统分类
     * 根据分类字典编码C06进行处理：如果不存在则新增分类字典，如果存在则先删除原有子项，然后将查询分类接口的数据作为新的子分类添加
     *
     * @return
     */
    @AutoLog(value = "同步存货分类到系统分类")
    @Operation(summary = "同步存货分类到系统分类")
    @PostMapping(value = "/syncInventoryClassToCategory")
    public Result<?> syncInventoryClassToCategory() {
        try {
            iTplusService.syncInventoryClassToCategory();
            return Result.OK("同步存货分类到系统分类成功！");
        } catch (Exception e) {
            return Result.error("同步存货分类到系统分类失败: " + e.getMessage());
        }
    }

    /**
     * 查询T+数据
     *
     * @param sql
     * @return
     */
    @AutoLog(value = "查询T+数据")
    @Operation(summary = "查询T+数据")
    @GetMapping(value = "/query")
    public Result<?> query(@RequestParam String sql) {
        List<Map<String, Object>> list = databaseService.queryFromSecondaryDatabase(sql);
        return Result.OK(list);
    }
}
